<?php

namespace Admin\Controller;

use Base\BackController;

class MultiUploadPicController extends BackController {

    public function looks(){
        $this->index();
    }

    public function checkAllPicUsed(){
        //轮询检查所有图片上会否还在被使用
        // $ckey='MultiUploadPic_checkAllPicUsed'.$_GET['type'];
        // $lastCheckTime=S($ckey);
        // if(!$lastCheckTime){
        //    $lastCheckTime=time()-65536; 
        // }
        // if(time()-$lastCheckTime>300){
            $picM=D('MultiUploadPic');
            $picM->checkAllPicUsed("",$_GET['seconds'],$_GET['count']);
            // S($ckey,time());
        // }
        //end 先实时检查所有图片上会否还在被使用
    }

    //批量上传图片列表
    public function index() {
        // //先实时检查所有图片上会否还在被使用
        // $ckey='MultiUploadPic_checkAllPicUsed'.$_GET['type'];
        // $lastCheckTime=S($ckey);
        // if(!$lastCheckTime){
        //    $lastCheckTime=time()-65536; 
        // }
        // if(time()-$lastCheckTime>300){
        //     $picM=D('MultiUploadPic');
        //     $picM->checkAllPicUsed($_GET['type']);
        //     S($ckey,time());
        // }
        // //end 先实时检查所有图片上会否还在被使用
        $this->user_model = M('bh_multi_upload_pic');

        $map=array();

        if($_GET['ori_name']!=''){
            $map['ori_name']=array('like', '%'.$_GET['ori_name'].'%');
        }
        if($_GET['type']!=''){
             $map['type']=$_GET['type'];
        } 
        if($_GET['is_using']!=''){
             $map['is_using']=$_GET['is_using'];
        }   

        //总数
        $total = $this->user_model->/*join($join)->join($joinad)->*/where($map)->count();
        //每页条数
        $listRows = C('LIST_ROWS') > 0 ? C('LIST_ROWS') : 10;
        //实例化分页类
        $page = new \Admin\ORG\PageNew($total, $listRows, '');
        //if ($total > $listRows) {
            $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        //}
        $p = $page->show();
        $this->assign('_page', $p ? $p : '');
        $dataList = $this->user_model->where($map)->order('id desc')
        ->limit("$page->firstRow,$page->listRows")->select();
        $img_multi_create_time=C('img_multi_create_time');
        //数据处理
        foreach ($dataList as $k => &$value){
            // cate
            if($value['type'] ==1){
                   $value['typeText'] ='<span style="color:red;">商品图</span>'; 
                   $subFolder="commodity";
            }else{
                   $value['typeText'] ='<span style="color:blue;">造型图</span>'; 
                   $subFolder="looks";
            }
            
            $value['uni_img'] =  ic_getUploadImgFullPath($subFolder,"s_".$value['uni_name'],$img_multi_create_time); 
            $value['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
            $value['update_time'] = date('Y-m-d H:i:s', $value['update_time']);


            
        }
        $this->assign('_list', $dataList);
        $this->assign('ori_name', $_GET['ori_name']);
        $this->assign('type', $_GET['type']);
        $this->assign('is_using', $_GET['is_using']);

        $this->assign('groupName', '批量图片管理');
        $this->assign('moduleName', '批量图片列表');
        $this->assign('actionName', '图片列表');

        $this->display('index');
    }
    
 
    private function delMulti($serverPath,$delName){
        @unlink($serverPath.$delName);
        @unlink($serverPath."m_".$delName);
        @unlink($serverPath."s_".$delName);
    }

    
    //删除规则
    public function deleteRule() {
        ic_rememberFormerPage();
        $ruleid = I('id');
        if (empty($ruleid)) {
            $this->error('缺失主键');
        }
        $exist=M('bh_multi_upload_pic')->where("id={$ruleid}")->find();
        if(!$exist){
            $this->error('待删除图片记录未找到，请检查是否已删除.');
        }
        //删除前先判断该图片是否被使用过
        $picM=D('MultiUploadPic');
        $used=$picM->findUsedRecords($exist['uni_name'],$exist['type']);
        if($used['used']){
            $this->error('该图片还在被'.$used['useby'].'使用，不能删除.');
        }
        //删除规则表
        
        $flag = M('bh_multi_upload_pic')->where("id={$ruleid}")->delete();
        if ($flag) {
            $img_multi_create_time=C('img_multi_create_time');
            // cate
            if($exist['type'] ==1){
                   $subFolder="commodity";
            }else{
                   $subFolder="looks";
            }
            //删除物理图片
            $serverPath=ic_getUploadImgServerPath($subFolder,$img_multi_create_time);
            $this->delMulti($serverPath,$exist['uni_name']);
            $this->success('删除成功',ic_backToFormerPage(__CONTROLLER__ . '/index'));
        } else {
            $this->error('删除失败');
        }
    }


   
}
